#include <windows.h>
#define ID_FILE_EXIT 9001
#define ID_STUFF_GO 9002
#define IDC_MAIN_BUTTON 1123
#define IDC_MAIN_EDIT 233
int value;
int temp =5 ;
const int ID_TIMER = 1;
const char g_szClassName[] = "myWindowClass";
const int BALL_MOVE_DELTA = 2;

// Step 4: the Window Procedure
void DrawBall(HWND hwnd,HDC hdc, RECT* prc)
{
	//////////////////PEN///////////////////////
	HPEN hPen; // pen dung de ve
	HPEN old_Pen; // luu lai but ve, sau do giai phong
	HBRUSH hBrush, old_hBrush;
	PAINTSTRUCT ps; 
	RECT rcClient;
	RECT balls;
	RECT fram;
	GetClientRect(hwnd,&rcClient);
	balls.top = rcClient.top +180+ value;
	balls.bottom = rcClient.bottom -180 + value;
	if(balls.top == rcClient.top)
		temp=5;
	if(balls.bottom == rcClient.bottom)
		temp=-5;
	value+=temp;
	//////////////////END///////////////////////
	balls.left = rcClient.left + 200;
	balls.right = rcClient.right - 200;
	balls.top = rcClient.top + 180 + value;
	balls.bottom = rcClient.bottom - 180 + value;

	fram.left = rcClient.left ;
	fram.right = rcClient.right ;
	fram.top = rcClient.top ;
	fram.bottom = rcClient.bottom;


	hPen = CreatePen(PS_SOLID,5, RGB(0,0,255));
	//hBrush = CreateSolidBrush(RGB(255,0,0));
	old_Pen = (HPEN)SelectObject(hdc, hPen);
	// FillRect (hdc, &balls, hBrush) ;
	//old_hBrush =(HBRUSH)SelectObject(hdc, hBrush);

	Rectangle(hdc,fram.left , fram.top,fram.right,fram.bottom);
	Ellipse	(hdc, balls.left , balls.top,	balls.right, balls.bottom);
	///////////////giai phong bo nho////////////////
	SelectObject(hdc, old_Pen);
	//SelectObject(hdc, old_hBrush);
	DeleteObject(hPen);
	//DeleteObject(hBrush);
	InvalidateRect(hwnd,&fram, 0); 
	    

}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {

	  case WM_CREATE:
		{
			// create menu
			HMENU hMenu, hSubMenu;
			HICON hIcon, hIconSm;
			hMenu = CreateMenu();
			hSubMenu = CreatePopupMenu();
			AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
			AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "&No");
			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");

			hSubMenu = CreatePopupMenu();
			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Option");
			AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "&Clear");
			SetMenu(hwnd, hMenu);
			// Load Icon
			hIcon =(HICON) LoadImage(NULL, "20.ico", IMAGE_ICON, 64, 64, LR_LOADFROMFILE);
			/*if(hIcon)
				SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
			else
				MessageBox(hwnd, "Could not load large icon!", "Error", MB_OK | MB_ICONERROR);

			hIconSm =(HICON) LoadImage(NULL, "20.ico", IMAGE_ICON, 64, 64, LR_LOADFROMFILE);
			if(hIconSm)
				SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
			else
				MessageBox(hwnd, "Could not load small icon!", "Error", MB_OK | MB_ICONERROR);
			*///button
			//HWND hWndButton=CreateWindowEx(NULL, "BUTTON","&OK",WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,50,50,100,100,hwnd,(HMENU)IDC_MAIN_BUTTON,GetModuleHandle(NULL),NULL);
			// create timer
			bool ret;
			ret = SetTimer(hwnd, ID_TIMER, 10, NULL);
			if(ret == 0)
				MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
		}
		break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
		case WM_TIMER:
			{				

				RECT rcClient;
				HDC hdc = GetDC(hwnd);
				GetClientRect(hwnd, &rcClient);
				DrawBall(hwnd,hdc, &rcClient);
				ReleaseDC(hwnd, hdc);
			}
			break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
		case WM_COMMAND:
		switch(LOWORD(wParam))
		{
			case IDC_MAIN_BUTTON:
			{

			}
			break;
		}
		break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_QUESTION);
    wc.hCursor       = LoadCursor(NULL, IDC_ICON);
    wc.hbrBackground = (HBRUSH)(COLOR_HIGHLIGHTTEXT+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_QUESTION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}